/*************************************************************************
	> File Name: handle.h
	> Author: ma6174
	> Mail: ma6174@163.com 
	> Created Time: 2021年12月09日 星期四 20时17分37秒
 ************************************************************************/

#include<iostream>
#include <string>
#include <typeinfo>

class Handler
{
public:
	Handler() = default;
	Handler(Handler* sc) : successor(sc) {}
	virtual ~Handler();

	void setSuccessor(Handler* sc);

	virtual void handleRequest(int request) = 0;

protected:
	Handler* successor;
};

Handler::~Handler()
{
	delete successor;
}

void Handler::setSuccessor(Handler* sc)
{
	successor = sc;
}

class ConcreteHandler1 : public Handler
{
public:
	using Handler::Handler;
	virtual void handleRequest(int request)
	{
		if (request >= 0 && request < 10)
			std::cout << std::string(typeid(*this).name()) << "\t处理请求\t" << request << std::endl;
		else if (successor != nullptr)
			successor->handleRequest(request);
	}
};

class ConcreteHandler2 : public Handler
{
public:
	using Handler::Handler;
	virtual void handleRequest(int request)
	{
		if (request >= 10 && request < 20)
			std::cout << std::string(typeid(*this).name()) << "\t处理请求\t" << request << std::endl;
		else if (successor != nullptr)
			successor->handleRequest(request);
	}
};

class ConcreteHandler3 : public Handler
{
public:
	using Handler::Handler;
	virtual void handleRequest(int request)
	{
		if (request >= 20 && request < 30)
			std::cout << std::string(typeid(*this).name()) << "\t处理请求\t" << request << std::endl;
		else if (successor != nullptr)
			successor->handleRequest(request);
	}
};
